#!/bin/bash
###SHELLPACK preamble openfoam OpenFOAM-v1812
GIT_LOCATION=https://develop.openfoam.com/Development/openfoam.git
MIRROR_LOCATION="$WEBROOT/openfoam/"

install-depends cmake gnuplot glu-devel flex		\
	libqt4-devel qt4-assistant-adp-devel qt4-x11-tools

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargEnd

###SHELLPACK git_fetch openfoam-${VERSION}.tar.gz openfoam-${VERSION}-installed
###SHELLPACK build_start openfoam-${VERSION}-installed
if [ ! -e ThirdParty ]; then
	rm -rf ThirdParty*
	echo Fetching ThirdParty
	file_fetch https://sourceforge.net/projects/openfoam/files/v2006/ThirdParty-v2006.tgz \
		$MIRROR_LOCATION/ThirdParty-v2006.tgz \
		../ThirdParty-v2006.tgz
	echo Expanding ThirdParty
	tar -xf ../ThirdParty-v2006.tgz
	mv ThirdParty-v2006 ThirdParty
fi

# Setup environment and check
source etc/bashrc || die "Failed to source etc/bashrc"
source etc/config.sh/functions || die "Failed to source etc/config.sh/functions"
cd "$WM_PROJECT_DIR" || die "Failed to change to WM_PROJECT_DIR"

DEPSDIR=$SHELLPACK_SOURCES/openfoamdeps-installed
DEPSDIR_ESCAPED=`echo "$DEPSDIR" | sed -e 's/\//\\\\\//g'`

# Configure path to prebuilt libs
sed -i -e "s/^boost_version=.*/boost_version=boost-system/"				etc/config.sh/CGAL   || die "Failed configure BOOST version"
sed -i -e "s/^export BOOST_ARCH_PATH=.*/export BOOST_ARCH_PATH=$DEPSDIR_ESCAPED/"	etc/config.sh/CGAL   || die "Failed configure BOOST arch path" 
sed -i -e "s/^cgal_version=.*/cgal_version=CGAL-system/"				etc/config.sh/CGAL   || die "Failed configure CGAL version"
sed -i -e "s/^export CGAL_ARCH_PATH=.*/export CGAL_ARCH_PATH=$DEPSDIR_ESCAPED/"		etc/config.sh/CGAL   || die "Failed configure CGAL arch path" 
sed -i -e "s/^METIS_VERSION=.*/METIS_VERSION=metis-system/"				etc/config.sh/metis  || die "Failed configure CGAL version"
sed -i -e "s/^export METIS_ARCH_PATH=.*/export METIS_ARCH_PATH=$DEPSDIR_ESCAPED/"	etc/config.sh/metis  || die "Failed configure METIS arch path" 

sed -i -e "s/^fftw_version=.*/fftw_version=fftw-system/"				etc/config.sh/FFTW   || die "Failed configure FFTW version"
sed -i -e "s/^export FFTW_ARCH_PATH=.*/export FFTW_ARCH_PATH=$DEPSDIR_ESCAPED/"		etc/config.sh/FFTW   || die "Failed configure FFTW arch path" 

# Configure third party dependencies
sed -i -e "s/^SCOTCH_VERSION=.*/SCOTCH_VERSION=scotch_$THIRDPARTY_SCOTCH_VERSION/"	etc/config.sh/scotch || die "Failed configure SCOTCH version"
sed -i -e "s/^KAHIP_VERSION=.*/KAHIP_VERSION=kahip-$THIRDPARTY_KAHIP_VERSION/"		etc/config.sh/kahip  || die "Failed configure KAHIP version"

echo Building Third Party
for BUILD in KAHIP SCOTCH; do
	./ThirdParty/make$BUILD || die "Failed to build make$BUILD"
done

source etc/config.sh/CGAL
source etc/config.sh/FFTW
source etc/config.sh/scotch
source etc/config.sh/metis

# Build openFOAM
foamSystemCheck || die "Failed foamSystemCheck"
./Allwmake -s -l -j || die "Failed to build Allwmake"
foamInstallationTest || die "Failed installation test"

# Configure MPI usage
sed -i -e 's/mpirun -np/mpirun $OPENFOAM_MPI_OPTS $MMTESTS_MPI_OPTS -np/' bin/tools/RunFunctions

echo openfoam installed successfully
